#!/bin/tcsh -ef
####
#
#############################################################################
#                                                                           #
# Title: Spot Intensity	(experimental)   				    #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 02/20/2006                                             #
# Last Modification: 02/20/2006                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 94
#
# MANUAL: This is a temporary script that you probably don't want to use...
#
# DISPLAY: SYM
# DISPLAY: lattice
# DISPLAY: secondlattice
# DISPLAY: realang
# DISPLAY: realcell
# DISPLAY: TLTANG
# DISPLAY: TLTAXIS
# DISPLAY: TLTAXA
# DISPLAY: TAXA
# DISPLAY: TANGL
# DISPLAY: imagesidelength
# DISPLAY: stepdigitizer
# DISPLAY: magnification
# DISPLAY: tempkeep
# DISPLAY: comment
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
#
set imagename = ""
set imagenumber = ""
set imagesidelength = ""
set magnification = ""
set stepdigitizer = ""
set RESMAX = ""
set RESMIN = ""
set ALAT = ""
set realang = ""
set realcell = ""
set lattice = ""
set secondlattice = ""
set TLTANG = ""
set TLTAXIS = ""
set SYM = ""
set tempkeep = ""
#
#$end_vars
#
echo "<<@progress: 1>>"
#
source ${proc_2dx}/initialize
#
echo lattice = ${lattice}
echo secondlattice = ${secondlattice}
#
set scriptname = 2dx_fourierSpots
#

\rm -f LOGS/${scriptname}.results
#
source ${proc_2dx}/2dx_makedirs 
#
${proc_2dx}/linblock "Lattice is currently: ${lattice}"

set spotname = "SCRATCH/statistics.txt"

\rm -f ${spotname}
\rm -f SCRATCH/${imagename}_stat.txt
\rm -f SCRATCH/${imagename}_stat.csv

#
set imagecenterx = `echo ${imagesidelength} | awk '{ s = int( $1 / 2 ) } END { print s }'`
set imagecentery = ${imagecenterx}
#

if(-e FFTIR/${imagename}_fft.mrc) then

  ${bin_2dx}/2dx_mmboxa.exe << eot
FFTIR/${imagename}_fft.mrc
${imagenumber}
Y                               ! Use grid units?
Y                               ! Generate grid from lattice?
N                               ! Generate points from lattice?
2 1 250 15 15 12 12               ! IPIXEL,IOUT,NUMSPOT,NOH,NOK,NHOR,NVERT
${spotname} 
${imagecenterx} ${imagecentery}         ! XORIG,YORIG
${RESMIN} ${RESMAX} 1 ${realcell} ${ALAT} ${realang} ! RINNER,ROUTER,IRAD,A,B,W,ABANG
${lattice}                         ! Lattice vectors
eot

echo "Lattice Statistices For:,${imagename}" > SCRATCH/${imagename}_stat.txt
echo "Reciprocal Lattice:,$lattice" >> SCRATCH/${imagename}_stat.txt
echo "Real Cell:,${realcell}" >> SCRATCH/${imagename}_stat.txt
echo "Real Cell Angle:,${realang}" >> SCRATCH/${imagename}_stat.txt
echo "" >> SCRATCH/${imagename}_stat.txt
echo "" >> SCRATCH/${imagename}_stat.txt

echo "h,k,Amplitude,Phase,RMS Background" >> SCRATCH/${imagename}_stat.txt
cat ${spotname} | awk ' { if(NR>1) {print $1","$2","$3","$4","$6} } ' >> SCRATCH/${imagename}_stat.txt
cp SCRATCH/${imagename}_stat.txt SCRATCH/${imagename}_stat.csv
\rm ${spotname}
echo "IMAGE-IMPORTANT: SCRATCH/${imagename}_stat.txt" "<Spot Statistics>" >> LOGS/${scriptname}.results
echo "IMAGE-IMPORTANT: SCRATCH/${imagename}_stat.csv" "<Spot Statistics (Excel)>" >> LOGS/${scriptname}.results
else
${proc_2dx}/linblock "Can't find FFTIR/${imagename}_fft.mrc"
endif 
echo "<<@progress: 100>>"
  
##########################################################################
${proc_2dx}/linblock "${scriptname} - normal end."
##########################################################################

